<?php

require_once DZ_ROOT . 'table/table_common_member.php';
require_once DZ_ROOT . 'table/table_common_member_openid.php';

class DiscuzUserDAO {    
    public $member = array();

    public $mapping = array();

    function __construct() {
    	msg("Init DiscuzUserDAO");
        $this->member = new table_common_member();
        $this->mapping = new table_common_member_openid();
    }

    public function getDZUser($openid) {
        $result = $this->mapping->fetch_openid($openid);
        if(!is_null($result)) {
            msg("Find mapping");
            $uid = $result['uid'];
            $usernames = $this->member->fetch_all_username_by_uid(array($uid));
            $basic = $this->member->fetch_by_username($usernames[$uid]);
            if(ISGBK) {
            	$result['realname'] = iconv("GB2312", "UTF-8", $result['realname']);
            	$basic['username'] = iconv("GB2312", "UTF-8", $basic['username']);
            	$result['address'] = iconv("GB2312", "UTF-8", $result['address']);
            }
            return array(
            	'uid' => $uid,
            	'username' => $basic['username'],
            	'realname' => $result['realname'],
            	'openid' => $result['openid'],
            	'fid' => $result['fid'],
            	'tid' => $result['tid'],
            	'headURL' => $result['headURL'],
            	'gender' => $result['gender'],
            	'address' => $result['address'],
            	'isTeacher' => !empty($basic['password'])
            );
        }

        msg("No mapping find");
        return NULL;
    }

    public function createDZUser($userInfo) {
    	// Convert to gbk
    	if(ISGBK) {
    		$userInfo['nickname'] = iconv("UTF-8", "GB2312//IGNORE", $userInfo['nickname']);
    		$userInfo['city'] = iconv("UTF-8", "GB2312//IGNORE", $userInfo['city']);
    	}
    	msg("Create a DZ user: openid = " . 
             $userInfo['openid'] . " nickname = " .  $userInfo['nickname']);
        $extdata['profile'] = array(
            'site' => $userInfo['openid'],
        	'realname' => $userInfo['nickname'],
        	'gender' => $userInfo['gender'],
        	'address' => $userInfo['city'],
        	'icq' => $userInfo['headimgurl'],
        	'height' => "na",
        	'weight' => "na"
        );
        
        $uid = $this->getUID();
        $username = "微信" . $uid;
        if(ISGBK) {
        	$gbk_username = iconv("UTF-8", "GB2312//IGNORE", $username);
        }
        $this->member->insert(
            $uid,
            $gbk_username,
            "",
            "", #email
            "", #ip
            $groupid,
            $extdata
        );
        
        return array(
            	'uid' => $uid,
        		'username' => $username,
            	'realname' => $userInfo['nickname'],
            	'openid' => $userInfo['openid'],
            	'fid' => "na",
            	'tid' => "na",
            	'headURL' => $userInfo['headimgurl'],
            	'gender' => $userInfo['gender'],
            	'address' => $userInfo['city'],
        		'isTeacher' => false
            );;
    }
    
    public function updateTID($uid, $tid) {
    	$this->mapping->update_tid_by_uid($uid, $tid);
    	    	
    }
    
    public function updateFID($uid, $fid) {
    	$this->mapping->update_fid_by_uid($uid, $fid);    	
    }

    private function getUID() {
        return $this->member->max_uid() + 1;
    }
}

?>
